{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7f52ca8c",
   "metadata": {},
   "source": [
    "### 窗口的显示与关闭"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "43dc9753",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c852a300",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准备销毁窗口\n"
     ]
    }
   ],
   "source": [
    "#创建窗口\n",
    "cv2.namedWindow('window',cv2.WINDOW_NORMAL)\n",
    "#更改窗口的大小\n",
    "cv2.resizeWindow('window',800,600)\n",
    "#展示名字为window的窗口\n",
    "cv2.imshow('window',0)\n",
    "#0代表等待任意按键  其余数字代表等待多少毫米秒关闭\n",
    "#可以利用waitKey来销毁窗口，不用每次都重启python\n",
    "key = cv2.waitKey(0)\n",
    "if key & 0xFF == ord('q'):\n",
    "    print(\"准备销毁窗口\")\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3b970e6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "113"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算ascii码\n",
    "ord('q')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff6cfc85",
   "metadata": {},
   "source": [
    "### 读取图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a907a91c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9ac7fa98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 34,  40,  39],\n",
       "        [ 34,  40,  39],\n",
       "        [ 34,  40,  39],\n",
       "        ...,\n",
       "        [ 36,  44,  44],\n",
       "        [ 36,  44,  44],\n",
       "        [ 36,  44,  44]],\n",
       "\n",
       "       [[ 34,  40,  39],\n",
       "        [ 34,  40,  39],\n",
       "        [ 34,  40,  39],\n",
       "        ...,\n",
       "        [ 37,  45,  45],\n",
       "        [ 38,  46,  46],\n",
       "        [ 38,  46,  46]],\n",
       "\n",
       "       [[ 34,  40,  39],\n",
       "        [ 34,  40,  39],\n",
       "        [ 34,  40,  39],\n",
       "        ...,\n",
       "        [ 38,  46,  46],\n",
       "        [ 39,  47,  47],\n",
       "        [ 39,  47,  47]],\n",
       "\n",
       "       ...,\n",
       "\n",
       "       [[ 98, 101, 106],\n",
       "        [ 95,  98, 103],\n",
       "        [ 96,  99, 104],\n",
       "        ...,\n",
       "        [ 78,  81,  86],\n",
       "        [ 77,  80,  85],\n",
       "        [ 77,  80,  85]],\n",
       "\n",
       "       [[ 96,  99, 104],\n",
       "        [ 93,  96, 101],\n",
       "        [ 95,  98, 103],\n",
       "        ...,\n",
       "        [ 78,  81,  86],\n",
       "        [ 75,  78,  83],\n",
       "        [ 74,  77,  82]],\n",
       "\n",
       "       [[107, 110, 115],\n",
       "        [102, 105, 110],\n",
       "        [102, 105, 110],\n",
       "        ...,\n",
       "        [ 79,  82,  87],\n",
       "        [ 73,  76,  81],\n",
       "        [ 72,  75,  80]]], dtype=uint8)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#默认是按照彩色图片读取\n",
    "cat = cv2.imread('./cat.jpeg')\n",
    "cat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5e72034f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#与原图不一致，因为opencv读进来的图片数据是BGR\n",
    "# plt.imshow(cat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "204dc972",
   "metadata": {},
   "outputs": [],
   "source": [
    "cv2.imshow('cat',cat)\n",
    "key = cv2.waitKey(0)\n",
    "if key & 0xFF == ord('q'):\n",
    "    print(\"准备销毁窗口\")\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6ddacd9b",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-6-457228e9481d>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-6-457228e9481d>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    form utils import cv_show\u001b[0m\n\u001b[1;37m             ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "form utils import cv_show"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "feeaccc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "%run utils.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "28b07e4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "cv_show('cat',cat)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "488cf57c",
   "metadata": {},
   "source": [
    "### 保存图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "050ff64b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "\n",
    "cv2.namedWindow('img',cv2.WINDOW_NORMAL)\n",
    "cv2.resizeWindow('img',640,480)\n",
    "\n",
    "#读取图片\n",
    "img = cv2.imread('./cat.jpeg')\n",
    "\n",
    "while True:\n",
    "    cv2.imshow('img',img)\n",
    "    key = cv2.waitKey(0)\n",
    "    \n",
    "    if key == ord('q'):\n",
    "        break\n",
    "    if key == ord('s'):\n",
    "        cv2.imwrite('./123.png',img)\n",
    "    else:\n",
    "        print(key)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4896270",
   "metadata": {},
   "source": [
    "### 读取摄像头和视频数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "48a9534d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#视频其实本质上是由一幅一幅的图片组成的\n",
    "#24帧，30帧，60帧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4593bf48",
   "metadata": {},
   "outputs": [],
   "source": [
    "#打开摄像头\n",
    "import cv2\n",
    "\n",
    "cv2.namedWindow(\"video\",cv2.WINDOW_NORMAL)\n",
    "cv2.resizeWindow(\"video\",640,480)\n",
    "\n",
    "#如果没打开窗口，不会报错。会闪退\n",
    "cap = cv2.VideoCapture(0)\n",
    "#循环读取摄像头每一帧\n",
    "while True:\n",
    "    #读一帧数据，返回标记和这一帧数据，True表示读到了数据，False表示没读到数据\n",
    "    ret,frame = cap.read()\n",
    "    \n",
    "    #可以根据ret做个判断\n",
    "    if not ret:\n",
    "        #没读到数据，直接退出\n",
    "        break\n",
    "    cv2.imshow('video',frame)\n",
    "    \n",
    "    key = cv2.waitKey(1)\n",
    "    if key == ord('q'):\n",
    "        break\n",
    "#释放资源\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "70ecef72",
   "metadata": {},
   "outputs": [],
   "source": [
    "#打开摄像头\n",
    "import cv2\n",
    "\n",
    "cv2.namedWindow(\"video\",cv2.WINDOW_NORMAL)\n",
    "cv2.resizeWindow(\"video\",640,480)\n",
    "\n",
    "#如果没打开窗口，不会报错。会闪退\n",
    "cap = cv2.VideoCapture('./video.mp4')\n",
    "#循环读取摄像头每一帧\n",
    "while True:\n",
    "    #读一帧数据，返回标记和这一帧数据，True表示读到了数据，False表示没读到数据\n",
    "    ret,frame = cap.read()\n",
    "    \n",
    "    #可以根据ret做个判断\n",
    "    if not ret:\n",
    "        #没读到数据，直接退出\n",
    "        break\n",
    "    cv2.imshow('video',frame)\n",
    "    \n",
    "    #假如一个视频是30帧，那么每张图之间要隔多少毫秒\n",
    "    key = cv2.waitKey(1000//60)\n",
    "    if key == ord('q'):\n",
    "        break\n",
    "#释放资源\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c40f038",
   "metadata": {},
   "source": [
    "### 视频录制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ad8a7eb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "cap = cv2.VideoCapture(0)\n",
    "fourcc = cv2.VideoWriter_fourcc(*'mp4v')\n",
    "\n",
    "#创建videowriter\n",
    "vw = cv2.VideoWriter('output.mp4',fourcc,30,(640,480))\n",
    "while cap.isOpened():\n",
    "    ret,frame = cap.read()\n",
    "    if not ret:\n",
    "        break\n",
    "    vw.write(frame)\n",
    "    cv2.imshow('frame',frame)\n",
    "    \n",
    "    if cv2.waitKey(1) == ord('q'):\n",
    "        break\n",
    "\n",
    "cap.release()\n",
    "vw.release()\n",
    "cv2.destroyAllWindows()\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83cb5339",
   "metadata": {},
   "source": [
    "###控制鼠标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "672a4c8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 571 336 0 123\n",
      "0 498 307 0 123\n",
      "0 464 291 0 123\n",
      "0 439 280 0 123\n",
      "0 417 266 0 123\n",
      "0 400 252 0 123\n",
      "0 388 242 0 123\n",
      "0 375 225 0 123\n",
      "0 367 214 0 123\n",
      "0 353 190 0 123\n",
      "0 346 172 0 123\n",
      "0 345 158 0 123\n",
      "0 343 148 0 123\n",
      "0 342 144 0 123\n",
      "0 342 143 0 123\n",
      "0 341 145 0 123\n",
      "0 338 151 0 123\n",
      "0 336 155 0 123\n",
      "0 334 158 0 123\n",
      "0 334 159 0 123\n",
      "0 332 161 0 123\n",
      "0 331 163 0 123\n",
      "0 331 164 0 123\n",
      "0 330 165 0 123\n",
      "0 329 166 0 123\n",
      "0 329 167 0 123\n",
      "0 327 169 0 123\n",
      "0 319 175 0 123\n",
      "0 306 182 0 123\n",
      "0 289 189 0 123\n",
      "0 276 194 0 123\n",
      "0 259 199 0 123\n",
      "0 244 201 0 123\n",
      "0 240 201 0 123\n",
      "0 234 203 0 123\n",
      "0 232 203 0 123\n",
      "0 231 203 0 123\n",
      "0 230 203 0 123\n",
      "0 229 203 0 123\n",
      "0 229 200 0 123\n",
      "0 236 195 0 123\n",
      "0 249 189 0 123\n",
      "0 269 182 0 123\n",
      "0 289 179 0 123\n",
      "0 309 179 0 123\n",
      "0 324 179 0 123\n",
      "0 337 180 0 123\n",
      "0 344 183 0 123\n",
      "0 349 187 0 123\n",
      "0 352 190 0 123\n",
      "0 352 192 0 123\n",
      "0 353 193 0 123\n",
      "0 353 195 0 123\n",
      "0 353 198 0 123\n",
      "0 353 200 0 123\n",
      "0 353 203 0 123\n",
      "0 354 205 0 123\n",
      "1 354 205 1 123\n",
      "4 354 205 0 123\n",
      "0 354 205 0 123\n",
      "0 354 206 0 123\n",
      "0 343 210 0 123\n",
      "0 332 212 0 123\n",
      "0 318 216 0 123\n",
      "0 305 217 0 123\n",
      "0 294 217 0 123\n",
      "0 287 217 0 123\n",
      "0 286 217 0 123\n",
      "0 285 217 0 123\n",
      "0 284 217 0 123\n",
      "1 284 217 1 123\n",
      "4 284 217 0 123\n",
      "0 284 217 0 123\n",
      "7 284 217 1 123\n",
      "4 284 217 0 123\n",
      "1 284 217 17 123\n",
      "4 284 217 16 123\n",
      "0 284 217 16 123\n",
      "7 284 217 17 123\n",
      "4 284 217 16 123\n",
      "1 284 217 17 123\n",
      "4 284 217 16 123\n",
      "0 284 217 16 123\n",
      "0 284 216 0 123\n",
      "0 285 216 0 123\n",
      "0 287 216 0 123\n",
      "0 288 215 0 123\n",
      "0 294 212 0 123\n",
      "0 295 211 0 123\n",
      "0 299 210 0 123\n",
      "0 305 206 0 123\n",
      "0 309 203 0 123\n",
      "0 314 201 0 123\n",
      "0 316 200 0 123\n",
      "0 318 199 0 123\n",
      "2 318 199 2 123\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "#函数名随便，参数五个\n",
    "#event表示鼠标时间，xy是鼠标的坐标\n",
    "# flags鼠标的组合按键\n",
    "def mouse_callback(event,x,y,flags,userdata):\n",
    "    print(event,x,y,flags,userdata)\n",
    "    if event == 2:\n",
    "        cv2.destroyAllWindows()\n",
    "    \n",
    "cv2.namedWindow('mouse',cv2.WINDOW_NORMAL)\n",
    "cv2.resizeWindow('mouse',640,360)\n",
    "\n",
    "#设置鼠标的回调函数\n",
    "cv2.setMouseCallback('mouse',mouse_callback,'123')\n",
    "#生成全黑的图片\n",
    "img = np.zeros((360,640,3),np.uint8)\n",
    "while True:\n",
    "    cv2.imshow('mouse',img)\n",
    "    key = cv2.waitKey(1)\n",
    "    if key == ord('q'):\n",
    "        break\n",
    "cv2.destroyAllWindows()\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "918284a1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
